﻿using System;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Administration.UI;
using ConfigurationManagement.Administration.UI.Import;
using ConfigurationManagement.Administration.UI.Utilities;
using ConfigurationManagement.Data;

namespace ConfigurationManagement.Administration
{
    public partial class Host : Form
    {
        public Host()
        {
            InitializeComponent();
        }

        private void OpenForm(Form frm)
        {
            var canOpen = true;
            foreach (var child in MdiChildren.Where(child => frm.GetType() == child.GetType()))
            {
                canOpen = false;
                child.Activate();
            }

            if (!canOpen) return;
            frm.MdiParent = this;
            frm.Show();
        }


        private void ExitToolsStripMenuItemClick(object sender, EventArgs e)
        {
            Close();
        }


        private void CascadeToolStripMenuItemClick(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItemClick(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItemClick(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItemClick(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItemClick(object sender, EventArgs e)
        {
            foreach (var childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void ProjectsToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ManageProjectsForm());
        }

        private void EnvironmentsToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ManageEnvironmentsForm());
        }

        private void SolutionsToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ManageSolutionsForm());
        }

        private void SolutionDictionaryToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ManageSolutionDictionaryForm());
        }

        private void EnvironmentSettingsToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ManageSolutionDictionaryValuesForm());
        }

        public void UpdateToolString()
        {
            ConnectionStringToolStripLabel.Text = @"Connection String: " +
                                                  ConfigurationManager.ConnectionStrings[Constants.ConnectionStringKey].
                                                      ConnectionString;
        }

        private void HostLoad(object sender, EventArgs e)
        {
            if (DesignMode) return;
            UpdateToolString();
            var ctx = new ConfigurationManagementDataContext();
            if (!ctx.DatabaseExists())
            {
                MessageBox.Show(@"The application is configured to a database which does not exist");
            }
        }

        private void ImportFileBasedDictionaryToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ImportXmlDictionaryForm());
        }

        private void ExportDictionaryToFileToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ExportXmlDictionaryForm());
        }

        private void CopyProjectToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new CopyProjectForm());
        }

        private void CopySolutionToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new CopySolutionForm());
        }

        private void ChangeConnectionToolStripMenuItem1Click(object sender, EventArgs e)
        {
            OpenForm(new ConnectionStringForm());
        }

        private void ProduceSettingsReportToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ProduceConfigurationSettingsReportForm());
        }

        private void TestConfigureFileToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new ConfigureFileBasedOnDatabaseForm());
        }

        private void CopyEnvironmentToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new CopyEnvironmentForm());
        }

        private void ProduceTagUsageReportToolStripMenuItemClick(object sender, EventArgs e)
        {
            OpenForm(new TagUsageReportForm());
        }
    }
}